# Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
# This source file is part of the Cangjie project, licensed under Apache-2.0
# with Runtime Library Exception.
#
# See https://cangjie-lang.cn/pages/LICENSE for license information.

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../third_party/boundscheck-v1.1.16/include)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)

set(BACKEND "cjnative")
if(CANGJIELIB_IS_CJNATIVE_BACKEND)
    set(BACKEND "cjnative")
endif()

add_subdirectory(std)
add_subdirectory(dynamicLoader)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")
# cj codes are used only when building libs(.so, .a, .cjo etc.),
# so it should be controlled by CANGJIE_BUILD_STDLIB.
if(CANGJIE_CODEGEN_CJNATIVE_BACKEND)
    include(MakeCJNATIVEStdSharedLib)
    include(AddCJNATIVELibrary)
endif()

if(CANGJIE_CODEGEN_CJNATIVE_BACKEND)
    set(CJNATIVE_BACKEND "cjnative")
    set(libname stdFFI)
    add_library(${libname} SHARED ${CANGJIE_CORE_FFI_SRC})
    if(CMAKE_BUILD_TYPE MATCHES Release)
        target_link_options( ${libname} PRIVATE ${LINK_FLAGS} ${STRIP_FLAG} ${useld})
    else()
        target_link_options(${libname} PRIVATE ${LINK_FLAGS} ${useld})
    endif()
    target_link_directories(${libname} PRIVATE ${CMAKE_BINARY_DIR}/lib
        PRIVATE ${CMAKE_BINARY_DIR}/lib/${TARGET_TRIPLE_DIRECTORY_PREFIX}_${CJNATIVE_BACKEND}
        PRIVATE ${CMAKE_BINARY_DIR}/runtime/lib/${TARGET_TRIPLE_DIRECTORY_PREFIX}_${CJNATIVE_BACKEND})
    set(pcre2_lib "pcre2-8")
    if(DARWIN)
        set(pcre2_lib -L${CMAKE_BINARY_DIR}/third_party/pcre2/lib -l${pcre2_lib})
    elseif(MINGW)
        set(pcre2_lib -L${CMAKE_BINARY_DIR}/third_party/pcre2/lib -l:lib${pcre2_lib}${CMAKE_SHARED_LIBRARY_SUFFIX}.a)
    else()
        set(pcre2_lib -L${CMAKE_BINARY_DIR}/third_party/pcre2/lib -l:lib${pcre2_lib}${CMAKE_SHARED_LIBRARY_SUFFIX})
    endif()
    target_link_libraries(${libname} boundscheck)
    target_link_libraries(${libname} ${CORE_FFI_OBJECTS_LIST})
    target_link_libraries(${libname} cangjie-std-mathFFI-objs)
    target_link_libraries(${libname} cangjie-std-convertFFI-objs)
    target_link_libraries(${libname} cangjie-std-fsFFI-objs)
    target_link_libraries(${libname} cangjie-std-envFFI-objs)
    target_link_libraries(${libname} cangjie-std-posixFFI-objs)
    target_link_libraries(${libname} cangjie-std-processFFI-objs)
    target_link_libraries(${libname} cangjie-std-randomFFI-objs)
    target_link_libraries(${libname} cangjie-std-regexFFI-objs)
    target_link_libraries(${libname} cangjie-std-netFFI-objs)
    target_link_libraries(${libname} cangjie-std-timeFFI-objs)
    target_link_libraries(${libname} cangjie-std-unittestFFI-objs)
    target_link_libraries(${libname} cangjie-std-objectpoolFFI-objs)
    target_link_libraries(${libname} cangjie-std-runtimeFFI-objs)
    target_link_libraries(${libname} cangjie-std-regexFFI)
    target_link_libraries(${libname} cangjie-dynamicLoader-opensslFFI)
    target_link_libraries(${libname} ${pcre2_lib})
    add_dependencies(${libname} modify_typetemplate_object)
    target_link_libraries(${libname} m)

    if(CANGJIE_CODEGEN_CJNATIVE_BACKEND)
        if(DARWIN)
            target_link_libraries(${libname} "-Wl,-force_load,${RUNTIME_COMMON_LIB_DIR}/libcangjie-aio.a")
        else()
            target_link_libraries(${libname} "-Wl,--whole-archive" ${RUNTIME_COMMON_LIB_DIR}/libcangjie-aio.a "-Wl,--no-whole-archive" )
        endif()
        set(target_system_name ${CMAKE_SYSTEM_NAME})
        if(OHOS)
            set(target_system_name "linux_ohos")
        endif()
        string(TOLOWER "${target_system_name}_${CMAKE_SYSTEM_PROCESSOR}_${CJNATIVE_BACKEND}" tmpdir)
        target_link_directories(${libname} PRIVATE ${RUNTIME_COMMON_LIB_DIR}/../../runtime/lib/${tmpdir})
        set(runtime_lib cangjie-runtime)
        if(MINGW)
            set(runtime_lib -L${RUNTIME_COMMON_LIB_DIR} -l:libcangjie-runtime.dll.a)
        endif()
        target_link_libraries(${libname} ${runtime_lib})
    endif()
    target_link_libraries(${libname} ${OPENSSL_SSL_LINK_OPTIONS})
    target_link_libraries(${libname} ${OPENSSL_CRYPTO_LINK_OPTIONS})
    if(MINGW)
        target_link_libraries(${libname} crypt32)
        target_link_libraries(${libname} ws2_32)
    endif()
    set(tmpdir "lib")
    install(TARGETS ${libname} DESTINATION lib)
endif()

